package com.hddf.project.auth.service;

import java.util.List;

import org.hibernate.Session;
import org.kommii.mysql_conn_pool.SessionProvider;

import com.hddf.project.auth.dao.AuthorityDao;
import com.hddf.project.auth.dao_impl.AuthorityDaoImpl;
import com.hddf.project.auth.pojos.Authority;

public class AuthService {
	
	private static AuthService auth_service = null;
	private static AuthorityDao auth_dao = null;
	
	static {
		if(auth_service==null){
			auth_service = new AuthService();
		}
		if(auth_dao==null){
			auth_dao = new AuthorityDaoImpl();
		}
	}
	
	public static AuthService getInstance(){
		return auth_service;
	}
	
	public List<Authority> getAuthList(String start,String limit,int state){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			List<Authority> list = auth_dao.getAuthorities(start, limit, state, s);
			SessionProvider.commitSession(s);
			return list; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return null;
		}
	}
	
	public int getAuthTotalCount(int state){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			int totalCount = auth_dao.getAuthoritiesTotalCount(state, s);
			SessionProvider.commitSession(s);
			return totalCount; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return -1;
		}
	}
	
	
	public boolean addAuth(Authority auth){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			boolean rs = auth_dao.save(auth, s);
			SessionProvider.commitSession(s);
			return rs; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean deleteAuth(int id){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			boolean rs = auth_dao.delete(id, s);
			SessionProvider.commitSession(s);
			return rs; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean updateAuth(Authority auth){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			boolean rs = auth_dao.update(auth, s);
			SessionProvider.commitSession(s);
			return rs; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean setAuthOn(int id){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			boolean rs = auth_dao.setAuthOn(id, s);
			SessionProvider.commitSession(s);
			return rs; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean setAuthOff(int id){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			boolean rs = auth_dao.setAuthOff(id, s);
			SessionProvider.commitSession(s);
			return rs; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return false;
		}
	}
	
	public Authority getAuthById(int id){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			Authority auth = auth_dao.getAuthById(id, s);
			SessionProvider.commitSession(s);
			return auth; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return null;
		}
	}
	
	public Authority getAuthByName(String name){
		Session s = null;
		try{
			s = SessionProvider.getSession();
			Authority auth = auth_dao.getAuthByName(name, s);
			SessionProvider.commitSession(s);
			return auth; 
		}catch(Exception e){
			SessionProvider.rollback(s);
			e.printStackTrace();
			return null;
		}
	}

}
